﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Web.Helper
{
    public static class MvcHtmlExtensions
    {
        // Methods
        public static IHtmlString Button(this HtmlHelper html, string id, string buttonText, ButtonType type = 0, string iconClass = "", string url = "#", bool emptyText = false, object htmlAttributes = null)
        {
            TagBuilder builder = new TagBuilder("a");
            builder.GenerateId(id);
            builder.MergeAttribute("href", url);
            if (type == ButtonType.Plain)
            {
                builder.AddCssClass("l-btn-plain");
            }
            builder.AddCssClass("l-btn");
            TagBuilder builder2 = new TagBuilder("span");
            builder2.AddCssClass("l-btn-left");
            TagBuilder builder3 = new TagBuilder("span");
            if (!string.IsNullOrEmpty(iconClass))
            {
                builder3.AddCssClass(iconClass);
                builder3.AddCssClass("l-btn-text");
            }
            else
            {
                builder3.AddCssClass("l-btn-text-noIcon");
            }
            if (emptyText)
            {
                builder.MergeAttribute("title", buttonText);
                builder3.InnerHtml = "&nbsp;";
            }
            else
            {
                builder3.SetInnerText(buttonText);
            }
            builder2.InnerHtml = builder3.ToString();
            builder.InnerHtml = builder2.ToString();
            builder.MergeAttributes<string, object>(new RouteValueDictionary(htmlAttributes));
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
        }

        public static IHtmlString DivPopup(this HtmlHelper html, string id, string title, string messageText = "", IEnumerable<IHtmlString> buttons = null)
        {
            TagBuilder builder = new TagBuilder("div");
            builder.GenerateId(id);
            builder.AddCssClass("popupContainer");
            builder.MergeAttribute("style", "display: none;");
            builder.MergeAttribute("title", title);
            string str = "&nbsp;";
            TagBuilder builder2 = new TagBuilder("div");
            builder2.GenerateId("content");
            if (!string.IsNullOrEmpty(messageText))
            {
                TagBuilder builder3 = new TagBuilder("p");
                builder3.SetInnerText(messageText);
                builder2.InnerHtml = builder3.ToString();
            }
            builder.InnerHtml = builder2.ToString();
            if ((buttons != null) && (buttons.Count<IHtmlString>() > 0))
            {
                TagBuilder builder4 = new TagBuilder("div");
                builder4.AddCssClass("popupButtonsContainer");
                foreach (IHtmlString str2 in buttons)
                {
                    builder4.InnerHtml = builder4.InnerHtml + str2.ToHtmlString() + str;
                }
                builder.InnerHtml = builder.InnerHtml + builder4.ToString();
            }
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
        }

        public static IHtmlString GridToolbar(this HtmlHelper html, IEnumerable<IHtmlString> leftButtons = null, IEnumerable<IHtmlString> rightButtons = null)
        {
            TagBuilder builder = new TagBuilder("div");
            builder.AddCssClass("toolbarButtons");
            string str = "&nbsp;";
            if ((leftButtons != null) && (leftButtons.Count<IHtmlString>() > 0))
            {
                TagBuilder builder1 = new TagBuilder("div");
                builder1.AddCssClass("toolbarButtonsLeft");
                foreach (IHtmlString str2 in leftButtons)
                {
                    builder1.InnerHtml = builder1.InnerHtml + str2.ToHtmlString();
                }
                builder.InnerHtml = builder.InnerHtml + builder1.ToString() + str;
            }
            if ((rightButtons != null) && (rightButtons.Count<IHtmlString>() > 0))
            {
                TagBuilder builder2 = new TagBuilder("div");
                builder2.AddCssClass("toolbarButtonsRight");
                foreach (IHtmlString str2 in rightButtons)
                {
                    builder2.InnerHtml = builder2.InnerHtml + str2.ToHtmlString();
                }
                builder.InnerHtml = builder.InnerHtml + builder2.ToString() + str;
            }
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
        }

        public static IHtmlString HelpButton(this HtmlHelper html, string id, string imageUrl, string helpContent = "", object htmlAttributes = null)
        {
            TagBuilder builder = new TagBuilder("img");
            builder.GenerateId(id);
            builder.AddCssClass("helpButton");
            builder.MergeAttribute("helpcontent", helpContent);
            builder.MergeAttribute("src", imageUrl);
            builder.MergeAttributes<string, object>(new RouteValueDictionary(htmlAttributes));
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
        }
    }
}